package com.mazaiting.instructions.math;

import com.mazaiting.instructions.base.NoOperandsInstruction;
import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtOperandStack;

/**
 * 加法指令
 *
 * @author mazaiting
 * @since 2021/8/14
 */
public class AddInstruction {

    /**
     * 双精度浮点 加法
     */
    public static class DAdd extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            double v1 = stack.popDouble();
            double v2 = stack.popDouble();
            double result = v1 + v2;
            stack.pushDouble(result);
        }

        @Override
        public String toString() {
            return "dadd";
        }
    }

    /**
     * 单精度浮点 加法
     */
    public static class FAdd extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            float v1 = stack.popFloat();
            float v2 = stack.popFloat();
            float result = v1 + v2;
            stack.pushFloat(result);
        }

        @Override
        public String toString() {
            return "Fadd";
        }
    }

    /**
     * 整型 加法
     */
    public static class IAdd extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            int v1 = stack.popInt();
            int v2 = stack.popInt();
            int result = v1 + v2;
            stack.pushInt(result);
        }

        @Override
        public String toString() {
            return "iadd";
        }
    }

    /**
     * 长整型 加法
     */
    public static class LAdd extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            long v1 = stack.popLong();
            long v2 = stack.popLong();
            long result = v1 + v2;
            stack.pushLong(result);
        }

        @Override
        public String toString() {
            return "ladd";
        }
    }
}
